package edu.math.chaos.itfunc;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Properties;

/**
 * User: Илья
 * Date: 08.11.13
 * Time: 15:03
 */
public class AppProperties {

	public static final String ITERATIONS = "iterations";
	public static final String WIDTH = "width";
	public static final String HEIGHT = "height";
	public static final String INIT_X = "initX";
	public static final String INIT_Y = "initY";
	public static final String INIT_MATRIX = "initMatrix";

	private static Properties properties;

	public static void load(String[] args) {
		if (args == null || args.length < 1) {
			return;
		}

		InputStream in = null;
		try {
			in = new BufferedInputStream(new FileInputStream(args[0]));
		} catch (Throwable e) {
			e.printStackTrace();
		}

		if (in == null) {
			return;
		}

		Properties props = new Properties();
		try {
			props.load(in);
		} catch (Throwable e) {
			e.printStackTrace();
			return;
		}

		properties = props;
	}

	public static String get(String key, String defaultValue) {
		if (key == null || properties == null) {
			return null;
		}

		String value = properties.getProperty(key);
		return value != null ? value : defaultValue;
	}

	public static String get(String key) {
		return get(key, null);
	}
}
